﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Collections.Generic;

namespace SharpGeo
{
    /// <summary>
    /// Represents an immutable ring of coordinates
    /// </summary>
    public class LinearRing :
        IEnumerable<ICoordinate>,
        IReadOnlyCoordinateCollection
    {
        #region Constructors

        /// <summary>
        /// Constructs a linear ring
        /// </summary>
        /// <param name="coordinates">The coordinates of the linear ring</param>
        public LinearRing(ReadOnlyCoordinateCollection coordinates)
        {
            _coordinates = coordinates;
        }

        /// <summary>
        /// Constructs a linear ring
        /// </summary>
        /// <param name="coordinates">The coordinates of the linear ring</param>
        public LinearRing(ICoordinate[] coordinates)
            : this(new ReadOnlyCoordinateCollection(coordinates))
        { }

        /// <summary>
        /// Constructs a new linear ring
        /// </summary>
        /// <param name="ordinates">A array of ordinates</param>
        /// <param name="numberOfAxes">The number of axes</param>
        public LinearRing(double[] ordinates, int numberOfAxes) :
            this(new ReadOnlyCoordinateCollection(ordinates, numberOfAxes))
        { }

        #endregion Constructors

        #region Fields

        private ReadOnlyCoordinateCollection _coordinates;

        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets the number of coordinates stored in this linear ring
        /// </summary>
        public int Count { get { return _coordinates.Count; } }

        /// <summary>
        /// Gets the number of axes in this linear ring
        /// </summary>
        public int NumberOfAxes
        {
            get { return _coordinates.NumberOfAxes; }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Gets the coordinate stored at the specified index
        /// </summary>
        /// <param name="index">The index to get the coordinate at</param>
        /// <returns>The ICoordinate stored at the specified index</returns>
        public ICoordinate GetValue(int index)
        {
            return _coordinates.GetValue(index);
        }

        /// <summary>
        /// Determines if the linear ring is wound in a clockwise manner
        /// </summary>
        /// <returns>True if the linear ring is clockwise</returns>
        public bool IsClockwise()
        {
            return _coordinates.IsClockwise();
        }

        /// <summary>
        /// Returns true if this linear ring contains the passed linear ring
        /// </summary>
        /// <param name="linearRing">The linear ring to test if it is contained in this linear ring</param>
        /// <returns>True if the passed linear ring is contained in this linear ring</returns>
        public bool Contains(LinearRing linearRing)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Calculates the envelope of this linear ring
        /// </summary>
        /// <returns>The calculated envelope</returns>
        public Envelope CalculateEnvelope()
        {
            return _coordinates.CalculateEnvelope();
        }

        #endregion Methods

        #region IEnumerator

        /// <summary>
        /// Returns an enumerator that enumerates the ICoordinates stored in this linear ring
        /// </summary>
        /// <returns>The enumerator</returns>
        public IEnumerator<ICoordinate> GetEnumerator()
        {
            return ((IEnumerable<ICoordinate>)_coordinates).GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return ((System.Collections.IEnumerable)_coordinates).GetEnumerator();
        }

        #endregion IEnumerator
    }
}